package nc.bs.ep.dj;

import nc.vo.ep.dj.DJZBItemVO;
import nc.vo.pub.VOStatus;

public class BillCodeGetter {
	private final String str_mark = "#";
    
	public boolean isMatedBillType(String billtype)
	{
		boolean blMatched = false;
		if(billtype!=null &&
		  (
		    billtype.equals("D0")
		 || billtype.equals("D1")	
		 || billtype.equals("D2")
		 || billtype.equals("D3")
		  )		)
		  {
			return true;
		  }
		
		return blMatched;
	}
	public boolean IsSys(DJZBItemVO[] itemvos)
	{
		boolean blSys=false;
		if(itemvos==null || itemvos.length==0)
			return false;
		for (int i = 0; i < itemvos.length; i++) {
			if(itemvos[i].getZyx23()!=null)
			{
				if(getFixedMark(itemvos[i].getZyx23()).equals(str_mark))
				{
					return true;
				}
			}
		}
		return blSys;
	}
	public Integer getMaxCode(DJZBItemVO[] itemvos)
	{
		Integer retMaxCode =  null;
		if(itemvos==null || itemvos.length==0)
			return null;
		//Integer tempMaxCode  = null;
		for (int i = 0; i < itemvos.length; i++) {
			String zyx23 = itemvos[i].getZyx23();
			if(zyx23!=null)
			{
			Integer tempCode = new Integer(getFixedString(zyx23));
			if(i==0)
			{
				retMaxCode=tempCode;
			}
			else
			{
				if(tempCode.compareTo(retMaxCode)>0)
				{
					retMaxCode=tempCode;
				}
			}
			}
		}
		return retMaxCode;
	}
	
	public void suplyItems(DJZBItemVO[] items)
	{
		if(items==null || items.length==0)
			return ;
		Integer intInitMaxCode =  getMaxCode(items);
		Integer tempMaxCode =  intInitMaxCode;
	  	for (int i = 0; i < items.length; i++) {
    		if(items[i].getStatus()==VOStatus.NEW ) {
            String tempSerieNumber = getSerieNumber(tempMaxCode+1);
            items[i].setZyx23(items[i].getDjbh()+tempSerieNumber);
            tempMaxCode = tempMaxCode+1;
    		}
    	}
		
	}
	public String getFixedString(String oriStr)
	{
		String retStirng= null;
		if(oriStr==null)
			return null;
		if(oriStr.length()<=5)
			return null;
		int iBeginPos = oriStr.length()-4;
		retStirng=oriStr.substring(iBeginPos, oriStr.length());
		return retStirng;
	}
	public String getFixedMark(String oriStr)
	{
		String retStirng= null;
		if(oriStr==null)
			return null;
		if(oriStr.length()<=4)
			return null;
		int iBeginPos = oriStr.length()-5;
		retStirng=oriStr.substring(iBeginPos, iBeginPos+str_mark.length());
		return retStirng;
	}
	
	public String getSerieNumber(Integer intCode)
	{
		if(intCode==null || intCode.toString().trim().length()==0)
			return "#0000";
		String retSerieNumber  = null;
		retSerieNumber = intCode.toString().trim();
		while(retSerieNumber.length()<4)
		{
			retSerieNumber = "0"+retSerieNumber;
		}
		retSerieNumber=str_mark+retSerieNumber;
		return retSerieNumber;
	}
}
